package com.jtlrm.ckd.huanxin;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.base.sdk.base.net.CommonObserver;
import com.base.sdk.base.net.LifeCycleEvent;
import com.base.sdk.util.ImageUtil;
import com.gyf.barlibrary.ImmersionBar;
import com.hyphenate.chatuidemo.DemoHelper;
import com.hyphenate.chatuidemo.db.NewGroupDao;
import com.hyphenate.chatuidemo.db.NewUserDao;
import com.hyphenate.chatuidemo.db.UserDao;
import com.hyphenate.chatuidemo.domain.NewGroupEntity;
import com.hyphenate.chatuidemo.domain.NewUserEntity;
import com.hyphenate.chatuidemo.ui.ConversationListFragment;
import com.hyphenate.chatuidemo.utils.UserUtil;
import com.hyphenate.easeui.ShowChatGroupInfoutil;
import com.hyphenate.easeui.ShowChatUserInfoUtil;
import com.hyphenate.easeui.UpdateUserInfoUtil;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.entity.ImUserEntity;
import com.jtlrm.ckd.mvp.model.SuiFangModel;
import com.jtlrm.ckd.mvp.model.dao.UserHelper;
import com.jtlrm.ckd.mvp.view.activity.MainActivity;

import io.reactivex.subjects.PublishSubject;

/**
 * 最近会话
 */
public class MyConversationFragment extends ConversationListFragment {
    PublishSubject<LifeCycleEvent> lifecycleSubject = PublishSubject.create();

    public static MyConversationFragment newInstance() {
        MyConversationFragment fragment = new MyConversationFragment();
        return fragment;
    }


    NewUserDao userDao = new NewUserDao();
    SuiFangModel suiFangModel = new SuiFangModel();

    @Override
    public void updateUnread() {
        ((MainActivity) getActivity()).updateUnreadLabel();
    }

    @Override
    protected ShowChatUserInfoUtil getShowUserInfo() {
        return userInfoUtil;
    }

    /**
     * 列表姓名头像的获取
     */
    ShowChatUserInfoUtil userInfoUtil = new ShowChatUserInfoUtil() {
        @Override
        public void showUserInfo(final String imId, final TextView name, final ImageView avatar) {
            final NewUserEntity entity = UserUtil.getInstance().getUserByImId(imId);
            if (entity != null && !TextUtils.isEmpty(entity.getName())) {
                name.setText(entity.getName() + "");
                ImageUtil.loadCircleAvatarImage(avatar, entity.getAvatar());
            } else {
                suiFangModel.queryUserInfoByIm(imId, new CommonObserver<ImUserEntity>() {
                    @Override
                    public void onError(int errType, String errMessage) {

                    }

                    @Override
                    public void onResult(ImUserEntity data) {
                        if (data != null) {
                            NewUserEntity newUserEntity = new NewUserEntity();
                            newUserEntity.setName(data.getName());
                            newUserEntity.setUserId(data.getId());
                            newUserEntity.setAvatar(data.getHeadPicture());
                            newUserEntity.setImId(imId);
                            newUserEntity.setRole(data.getRole());
                            newUserEntity.setRoleName(data.getRoleName());
                            name.setText(data.getName() + "");
                            ImageUtil.loadCircleAvatarImage(avatar, data.getHeadPicture());
                            userDao.saveContact(newUserEntity);
                        }
                    }
                }, lifecycleSubject);
                name.setText(imId);
                ImageUtil.loadCircleAvatarImage(avatar, "");

            }
        }

        @Override
        public void groupIcon(String groupId, ImageView groupIcon) {
            if (userHelper.isHospitalGroup(groupId)) {
                ImageUtil.loadImage(groupIcon, R.drawable.logo);
            } else {
                ImageUtil.loadImage(groupIcon, R.drawable.ease_group_icon);
            }
        }

    };



    UserHelper userHelper;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userHelper = UserHelper.getInstance(getContext());
    }

    @Override
    public void onPause() {
        lifecycleSubject.onNext(LifeCycleEvent.PAUSE);
        super.onPause();
    }

    @Override
    public void onStop() {
        lifecycleSubject.onNext(LifeCycleEvent.STOP);
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        lifecycleSubject.onNext(LifeCycleEvent.DESTROY);

    }

    NewGroupDao newGroupDao;

    @Override
    protected ShowChatGroupInfoutil getGroupInfo() {
        return new ShowChatGroupInfoutil() {
            @Override
            public String getGroupName(String groupId) {
                if (newGroupDao == null) {
                    newGroupDao = new NewGroupDao();
                }
                NewGroupEntity entity = newGroupDao.getGroupById(groupId);
                if (entity != null && entity.getName() != null) {
                    return entity.getName();
                }
                return groupId;
            }
        };
    }
}
